package com.sdy.supermarket.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.StringUtil;
import com.sdy.supermarket.biz.mapper.GsHomeManageMapper;
import com.sdy.supermarket.biz.model.GsHomeManage;
import com.sdy.supermarket.biz.model.GsResFeatrue;
import com.sdy.supermarket.biz.mapper.GsResFeatrueMapper;
import com.sdy.supermarket.biz.service.GsResFeatrueService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hyh
 * @since 2019-08-02
 */
@Slf4j
@Service
public class GsResFeatrueServiceImpl extends BaseServiceImpl<GsResFeatrue> implements GsResFeatrueService {
    @Resource
    private GsResFeatrueMapper gsResFeatrueMapper;
    @Resource
    private GsHomeManageMapper gsHomeManageMapper;

    @Override
    public IPage<GsResFeatrue> pageList(Page<GsResFeatrue> page, String name) {
        LambdaQueryWrapper<GsResFeatrue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(GsResFeatrue::getSort);
        if (StringUtil.isNotEmpty(name) && StringUtil.isNotBlank(name)) {
            queryWrapper.like(GsResFeatrue::getFeatureName, name);
        }
        IPage<GsResFeatrue> iPage = page(page, queryWrapper);
        return page;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int deleteById(Integer id) throws BizException {
        Assert.isNull(id, "ID为NULL");
        GsResFeatrue deleteResFeatrue = gsResFeatrueMapper.selectById(id);
        Assert.isNull(deleteResFeatrue, "找不到要删除的数据");
        // 查询出被删除数据排序后面的数据,更改它们的排序
        LambdaQueryWrapper<GsResFeatrue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.gt(GsResFeatrue::getSort, deleteResFeatrue.getSort());
        List<GsResFeatrue> updateSortList = gsResFeatrueMapper.selectList(queryWrapper);
        int batchUpdate = updateSortList.size() == 0 ? 1 : 0;
        if (updateSortList.size() != 0) {
            updateSortList.forEach(res -> res.setSort(res.getSort() - 1));
            batchUpdate = gsResFeatrueMapper.batchUpdate(updateSortList);
        }
        int deleteById = gsResFeatrueMapper.deleteById(id);
        int homeUpdate = gsHomeManageMapper.updateById(new GsHomeManage().setId(3).setLastUpdateTime(new Date()));
        if (batchUpdate == 0 || deleteById == 0 || homeUpdate == 0) {
            throw new BizException("删除失败");
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int sortUpOrDown(Integer id, Integer type) throws BizException {
        Assert.isNull(id, "ID为NULL");
        Assert.isNull(type, "TYPE为NULL");
        GsResFeatrue upOrDownRes = gsResFeatrueMapper.selectById(id);
        Assert.isNull(upOrDownRes, "找不到数据");
        LambdaQueryWrapper<GsResFeatrue> queryWrapper = new LambdaQueryWrapper<>();
        if (type == 0) {
            queryWrapper.orderByDesc(GsResFeatrue::getSort).lt(GsResFeatrue::getSort, upOrDownRes.getSort());
        }
        if (type == 1) {
            queryWrapper.orderByAsc(GsResFeatrue::getSort).gt(GsResFeatrue::getSort, upOrDownRes.getSort());
        }
        List<GsResFeatrue> changeResList = gsResFeatrueMapper.selectList(queryWrapper);
        if (changeResList.size() != 0 && changeResList.get(0) != null) {
            // 主动修改排序的res
            GsResFeatrue up = new GsResFeatrue();
            up.setId(id);
            up.setSort(changeResList.get(0).getSort());
            // 被动修改排序的res
            GsResFeatrue down = new GsResFeatrue();
            down.setId(changeResList.get(0).getId());
            down.setSort(upOrDownRes.getSort());
            int upStatus = gsResFeatrueMapper.updateById(up);
            int downStatus = gsResFeatrueMapper.updateById(down);
            int homeUpdate = gsHomeManageMapper.updateById(new GsHomeManage().setId(3).setLastUpdateTime(new Date()));
            if (upStatus == 0 || downStatus == 0 || homeUpdate == 0) {
                throw new BizException("修改失败");
            }
            return 1;
        }
        Assert.isTrue(true, "排序已经到达最高或者最低");
        return 0;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int saveOrUpdateRes(GsResFeatrue gsResFeatrue) throws BizException {
        Date now = new Date();
        if (gsResFeatrue.getId() == null) {
            List<GsResFeatrue> gsResFeatrues = gsResFeatrueMapper.selectList(new LambdaQueryWrapper<>());
            gsResFeatrue.setCreateTime(now);
            gsResFeatrue.setSort(gsResFeatrues.size() + 1);
        }
        if (gsResFeatrue.getId() != null) {
            gsResFeatrue.setSort(null);
            gsResFeatrue.setCreateTime(null);
        }
        boolean saveOrUpdate = saveOrUpdate(gsResFeatrue);
        int homeUpdate = gsHomeManageMapper.updateById(new GsHomeManage().setId(3).setLastUpdateTime(now));
        Assert.isTrue(!saveOrUpdate || homeUpdate == 0, "操作失败");
        return 1;
    }

    @Override
    public List<GsResFeatrue> listValidResFeature() {
        List<GsResFeatrue> list = lambdaQuery().eq(GsResFeatrue::getDisplayStatus, 0)
                .eq(GsResFeatrue::getDisplayStatus, 0)
                .orderByAsc(GsResFeatrue::getSort)
                .last("limit  0,4")
                .list();
        return list;
    }
}
